package co.yixiang.modules.system.service.impl;

import co.yixiang.modules.system.domain.ConsumerOrder;
import cn.hutool.core.convert.Convert;
import co.yixiang.common.service.impl.BaseServiceImpl;
import lombok.AllArgsConstructor;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import co.yixiang.common.utils.QueryHelpPlus;
import co.yixiang.utils.ValidationUtil;
import co.yixiang.utils.FileUtil;
import co.yixiang.modules.system.service.ConsumerOrderService;
import co.yixiang.modules.system.dto.ConsumerOrderDto;
import co.yixiang.modules.system.dto.queryCriteria.ConsumerOrderQueryCriteria;
import co.yixiang.modules.system.mapper.ConsumerOrderMapper;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
// 默认不使用缓存
//import org.springframework.cache.annotation.CacheConfig;
//import org.springframework.cache.annotation.CacheEvict;
//import org.springframework.cache.annotation.Cacheable;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import java.util.List;
import java.util.Map;
import java.io.IOException;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.LinkedHashMap;

/**
* @author zf
* @date 2020-08-15
*/
@Service
@AllArgsConstructor
//@CacheConfig(cacheNames = "consumerOrder")
@Transactional(propagation = Propagation.SUPPORTS, readOnly = true, rollbackFor = Exception.class)
public class ConsumerOrderServiceImpl extends BaseServiceImpl<ConsumerOrderMapper, ConsumerOrder> implements ConsumerOrderService {

    @Override
    //@Cacheable
    public Map<String, Object> queryAll(ConsumerOrderQueryCriteria criteria, Pageable pageable) {
        getPage(pageable);
        PageInfo<ConsumerOrder> page = new PageInfo<>(queryAll(criteria));
        Map<String, Object> map = new LinkedHashMap<>(2);
        map.put("content", Convert.toList(ConsumerOrderDto.class, page.getList()));
        map.put("totalElements", page.getTotal());
        return map;
    }


    @Override
    //@Cacheable
    public List<ConsumerOrder> queryAll(ConsumerOrderQueryCriteria criteria){
        return baseMapper.selectList(QueryHelpPlus.getPredicate(ConsumerOrder.class, criteria));
    }


    @Override
    public void download(List<ConsumerOrderDto> all, HttpServletResponse response) throws IOException {
        List<Map<String, Object>> list = new ArrayList<>();
        for (ConsumerOrderDto consumerOrder : all) {
            Map<String,Object> map = new LinkedHashMap<>();
            map.put("uuid唯一标识码", consumerOrder.getUnique());
            map.put("消费者ID", consumerOrder.getConsumerId());
            map.put("商铺ID", consumerOrder.getShopId());
            map.put("订单类型，同 shop 表 category 冗余处理", consumerOrder.getCategory());
            map.put("会员卡id", consumerOrder.getCardId());
            map.put("订单金额", consumerOrder.getAmount());
            map.put("优惠金额", consumerOrder.getDiscountAmount());
            map.put("实付金额", consumerOrder.getPayAmount());
            map.put("支付时间", consumerOrder.getPayDate());
            map.put("支付状态，1：未支付，2已取消，3已过期，4已支付 5已撤销（后台退款）6 已退款（客户申请退款，团队） 9已支付", consumerOrder.getPayStatus());
            map.put("创建时间", consumerOrder.getCreateDate());
            list.add(map);
        }
        FileUtil.downloadExcel(list, response);
    }
}
